﻿using System;

using DreamCube.Foundation.Basic.Utility;

namespace BGN.PlugIn.Client
{
    /// <summary>
    /// Sinfo系统的用户
    /// </summary>
    public class SinfoUserHelper
    {
        /// <summary>
        /// 用户状态的枚举值
        /// </summary>
        public enum UserStated
        {
            /// <summary>
            /// 用户名不存在
            /// </summary>
            NotExistUserName,

            /// <summary>
            /// 用户密码不对
            /// </summary>
            PasswordNotRight,

            /// <summary>
            /// 表示两个系统匹配用户成功
            /// </summary>
            Match
        }

        /// <summary>
        /// 更新用户数据
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static Boolean UpdateUser(Model.User user)
        {
            String webServiceUrl = SInfoWebService.GetWebserviceUrl();
            return Convert.ToBoolean(DreamCube.Foundation.Basic.Utility.MyWeb.InvokeWebService(webServiceUrl, "UpdateUser", new Object[] { user })); 
        }

        /// <summary>
        /// 根据登陆的用户名和密码判断sinfo系统中是否存在该用户
        /// </summary>
        /// <param name="userLoginName">sinfo系统的用户登陆名（布谷鸟系统的用户名）</param>
        /// <param name="password">用户密码</param>
        /// <param name="user">输出参数</param>
        /// <returns></returns>
        public static UserStated CheckUser(String userLoginName, String password, out Model.User user)
        {
            user = new Model.User();
            String webServiceUrl = SInfoWebService.GetWebserviceUrl();
            Object returnValue = DreamCube.Foundation.Basic.Utility.MyWeb.InvokeWebService(webServiceUrl, "GetUserByUserLoginName", new Object[] { userLoginName });
            if (returnValue == null) return UserStated.NotExistUserName;
            DreamCube.Foundation.Basic.Utility.MyObject.CopyValueTo(returnValue, user);
            if (user == null) return UserStated.NotExistUserName;
            if (user.PasswordPlainText != password) return UserStated.PasswordNotRight;
            return UserStated.Match;
        }
    }
}
